package org.luxor.commons.schedule.config;

import org.luxor.commons.schedule.aspect.ScheduledAspect;
import org.luxor.commons.schedule.service.IScheduleJobLogService;
import org.luxor.commons.schedule.service.IScheduleJobService;
import org.luxor.commons.schedule.service.impl.ScheduleJobLogNoneServiceImpl;
import org.luxor.commons.schedule.service.impl.ScheduleJobNoneServiceImpl;
import org.springframework.boot.autoconfigure.condition.ConditionalOnClass;
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
import org.springframework.boot.autoconfigure.quartz.QuartzProperties;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Import;
import org.springframework.scheduling.annotation.EnableScheduling;

/**
 * 任务调度器全局配置
 *
 * @author Mr.yan @date 2020/2/12
 */
@Configuration
@EnableScheduling
@EnableConfigurationProperties(QuartzProperties.class)
@Import({QuartzJdbcConfiguration.class, ScheduleConfiguration.class})
public class QuartzAutoConfig {

    @Bean
    @ConditionalOnMissingBean(IScheduleJobLogService.class)
    public IScheduleJobLogService scheduleJobLogService() {
        return new ScheduleJobLogNoneServiceImpl();
    }

    @Bean
    @ConditionalOnMissingBean(IScheduleJobService.class)
    public IScheduleJobService scheduleJobService() {
        return new ScheduleJobNoneServiceImpl();
    }

    /**
     * 初始化注解处理器
     */
    @Bean
    @ConditionalOnClass({IScheduleJobService.class})
    public ScheduledAnnotationBeanInitPostProcessor scheduledAnnotationBeanInitPostProcessor(IScheduleJobService scheduleJobService) {
        return new ScheduledAnnotationBeanInitPostProcessor(scheduleJobService);
    }

    /**
     * 任务执行方法AOP
     */
    @Bean
    @ConditionalOnMissingBean
    @ConditionalOnClass({ScheduledAnnotationBeanInitPostProcessor.class, IScheduleJobService.class, IScheduleJobLogService.class})
    public ScheduledAspect scheduledAspect(IScheduleJobService scheduleJobService, IScheduleJobLogService scheduleJobLogService) {
        return new ScheduledAspect(scheduleJobService, scheduleJobLogService);
    }

}
